package com.liwang.lanouhn.nvshengo.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.liwang.lanouhn.nvshengo.Info.CosmeticLiveInfo;
import com.liwang.lanouhn.nvshengo.R;
import com.liwang.lanouhn.nvshengo.adapter.CosmeticLiveAdapter;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 2016/10/10.
 */
public class CosmeticLiveFragment extends Fragment {
    private CosmeticLiveInfo cosmeticliveInfo;
    private RecyclerView xRecyclerView;
    private CosmeticLiveAdapter cosmeticLiveAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cosmeticlive, null);
        initView(view);
        getDataFromWeb();
        return view;
    }

    private void initView(View view) {
        xRecyclerView = (RecyclerView) view.findViewById(R.id.xRecyclerview);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),2);
        xRecyclerView.setLayoutManager(gridLayoutManager);
    }

    private void getDataFromWeb() {
        long time = System.currentTimeMillis();//获取系统时间的时间戳

        String str = String.valueOf(time);

        final String url = "https://a4.plu.cn/api/streams?start-index=0&max-results=30&game=0&sort-by=belle&version=3.7.0&device=4&packageId=1";
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url)

                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        cosmeticliveInfo = gson.fromJson(result, CosmeticLiveInfo.class);
                        if (cosmeticliveInfo.getData().getItems().size()> 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();

    }

    private  Handler handler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    Toast.makeText(getContext(), "网络请求失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    cosmeticLiveAdapter=new CosmeticLiveAdapter(getActivity(),cosmeticliveInfo);
                    xRecyclerView.setAdapter(cosmeticLiveAdapter);
                    break;
            }
        }
    };
    }





